package cn.com.xf.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.com.xf.dao.TblCheckUserDAO;
import cn.com.xf.dao.support.XFSqlMapClientDaoSupport;
import cn.com.xf.domain.TblCheckCmd;
import cn.com.xf.domain.TblCheckUser;

@Repository
public class TblCheckUserDAOImpl extends XFSqlMapClientDaoSupport implements TblCheckUserDAO {

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public TblCheckUserDAOImpl() {
        super();
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public int deleteByPrimaryKey(Long id) {
        TblCheckUser key = new TblCheckUser();
        key.setId(id);
        int rows = getSqlMapClientTemplate().delete("TBL_CHECK_USER.ibatorgenerated_deleteByPrimaryKey", key);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public void insert(TblCheckUser record) {
        getSqlMapClientTemplate().insert("TBL_CHECK_USER.ibatorgenerated_insert", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public void insertSelective(TblCheckUser record) {
        getSqlMapClientTemplate().insert("TBL_CHECK_USER.ibatorgenerated_insertSelective", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public TblCheckUser selectByPrimaryKey(Long id) {
        TblCheckUser key = new TblCheckUser();
        key.setId(id);
        TblCheckUser record = (TblCheckUser) getSqlMapClientTemplate().queryForObject("TBL_CHECK_USER.ibatorgenerated_selectByPrimaryKey", key);
        return record;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public int updateByPrimaryKeySelective(TblCheckUser record) {
        int rows = getSqlMapClientTemplate().update("TBL_CHECK_USER.ibatorgenerated_updateByPrimaryKeySelective", record);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_USER
     *
     * @ibatorgenerated Fri Nov 11 16:34:55 CST 2016
     */
    public int updateByPrimaryKey(TblCheckUser record) {
        int rows = getSqlMapClientTemplate().update("TBL_CHECK_USER.ibatorgenerated_updateByPrimaryKey", record);
        return rows;
    }

	@Override
	public List<TblCheckUser> selectUsers(int begin, int pageSize, String sn, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		int end = begin + pageSize;
		begin = begin + 1;
		map.put("begin", begin);
		map.put("end", end);
		map.put("sn", sn);
		map.put("userid", userid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_USER.selectUsers", map);
	}

	@Override
	public int selectCount(String sn, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sn", sn);
		map.put("userid", userid);
		return (Integer)getSqlMapClientTemplate().queryForObject("TBL_CHECK_USER.selectCount", map);
	}

	@Override
	public int insertBatch(List<TblCheckUser> users) {
		int count = 0;
		try {
			// 自己控制事物的开始
			getSqlMapClient().startTransaction();
			getSqlMapClient().startBatch();
			for (TblCheckUser user : users) {
				try {
					if (user.getId() == null) {
						getSqlMapClientTemplate().insert("TBL_CHECK_USER.ibatorgenerated_insertSelective", user);
					} else {
						int rows = getSqlMapClientTemplate().update("TBL_CHECK_USER.ibatorgenerated_updateByPrimaryKeySelective", user);
					}
					count = getSqlMapClient().executeBatch();
					getSqlMapClient().commitTransaction();
				} catch (SQLException e) {
				}
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 自己控制事物的结束
			try {
				getSqlMapClient().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}

	@Override
	public List<TblCheckUser> selectUser(String sn, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sn", sn);
		map.put("userid", userid);
		List<TblCheckUser> list = getSqlMapClientTemplate().queryForList("TBL_CHECK_USER.selectBySnAndUserid", map);
		return list;
	}
}